package com.jinzhi.jzweb.controller.modelConfig;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.modelConfig.DeclareDO;
import com.jinzhi.jzweb.domain.modelConfig.DeclareReadyDO;
import com.jinzhi.jzweb.service.modelConfig.DeclareReadyService;
import com.jinzhi.jzweb.service.modelConfig.DeclareService;
import com.jinzhi.sys.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.jinzhi.api.util.TokenTag.TOKEN_TAG;

/**
 * 
 * <pre>
 * 申报材料表
 * </pre>
 * <small> 2024-01-10 09:31:39 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/declareReady")
public class DeclareReadyController extends BaseController {
	@Autowired
	private DeclareReadyService declareReadyService;

	@Autowired
	private DeclareService declareService;


	/**
	 * 查询申报列表
	 * @param declareReadyDTO
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<DeclareReadyDO>> list(DeclareReadyDO declareReadyDTO){
		Page<DeclareReadyDO> page = declareReadyService.selectPage(declareReadyDTO,getPage(DeclareReadyDO.class));
        return Result.ok(page);
	}


	/**
	 * 前台查询申报列表
	 * @param declareReadyDTO
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getList")
	public Result<Page<DeclareReadyDO>> getList(DeclareReadyDO declareReadyDTO,@RequestHeader(value = TOKEN_TAG, required = false) String token){
		if(!StringUtil.notEmpty(token)){
			return Result.fail("token效验失败");
		}
		//1.根据token获取用户登录数据
		UserDO user = this.getClientUser(token);
		if(user == null){
			return Result.fail("用户效验失败");
		}
		Page<DeclareReadyDO> page = declareReadyService.selectPage(declareReadyDTO,getPage(DeclareReadyDO.class));
		for (int i = 0; i < page.getRecords().size(); i++) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			int year = Integer.parseInt(sdf.format(new Date()));
			List<DeclareDO> doList =  declareService.selectList(new EntityWrapper<DeclareDO>().eq("creater_id",user.getId())
					.eq("declare_ready_id",page.getRecords().get(i).getId())
					.andNew("YEAR(creater_date) = {0}",year)
					.orderBy("creater_date",false));
			if(doList.isEmpty()){
				page.getRecords().get(i).setDeclareStatus(5);
			}else{
				page.getRecords().get(i).setDeclareStatus(doList.get(0).getStatus());
			}
		}
		return Result.ok(page);
	}

    /**
     * 查询申报byid
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/selectDeclareReadyById")
    public Result<DeclareReadyDO> selectDeclareReadyById(Long id){
		DeclareReadyDO declareReadyDO = declareReadyService.selectById(id);
        return Result.ok(declareReadyDO);
    }

	@GetMapping("/add")
	@RequiresPermissions("jzweb:declareReady:add")
	String add(){
	    return "jzweb/declareReady/add";
	}

	@Log("添加申报材料表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:declareReady:add")
	public Result<String> save( DeclareReadyDO declareReady){
		declareReady.setCreaterId(this.getUserId());
		boolean save = declareReadyService.insertDeclareReady(declareReady);
		return save ? Result.ok() : Result.fail();
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:declareReady:edit")
	String edit(@PathVariable("id") Long id,Model model){
		DeclareReadyDO declareReady = declareReadyService.selectById(id);
		model.addAttribute("declareReady", declareReady);
	    return "jzweb/declareReady/edit";
	}

	@Log("申报材料表详情")
	@GetMapping("/details/{id}")
	@RequiresPermissions("jzweb:declareReady:edit")
	String details(@PathVariable("id") Long id,Model model){
		DeclareReadyDO declareReady = declareReadyService.selectById(id);
		model.addAttribute("declareReady", declareReady);
		return "jzweb/declareReady/details";
	}

	/**
	 * 查询详情列表
	 * @param declareReadyDTO
	 * @return
	 */
	@ResponseBody
	@GetMapping("/declarelist")
	@RequiresPermissions("jzweb:declareReady:declareReady")
	public Result<Page<DeclareDO>> declarelist(DeclareReadyDO declareReadyDTO){
		EntityWrapper wrapper = new EntityWrapper();
		wrapper.eq("declare_ready_id",declareReadyDTO.getId());
		Page<DeclareDO> page = declareService.selectPage(getPage(DeclareDO.class),wrapper);
		return Result.ok(page);
	}

	@Log("修改申报材料表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:declareReady:edit")
	public Result<String>  update( DeclareReadyDO declareReady){
		boolean update = declareReadyService.updateDeclareReadyById(declareReady);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除申报材料表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:declareReady:remove")
	public Result<String>  remove( Long id){
		boolean delete = declareReadyService.deleteDeclareReadyById(id);
        return delete ? Result.ok() : Result.fail();
	}

	
}
